﻿using System;
using System.ComponentModel;

namespace HobSoft.SoccerBet2014.Domain.Common.SiteContext
{
    using sm = Domain.Models.Site;
    public class Context : ContextBase, INotifyPropertyChanged, INotifyPropertyChanging
    {
        public Context(sm.SiteContext persistent)
            : base(persistent)
        {

        }

        private string _value;

        public string Value
        {
            get { return _value; }
            set
            {
                if (_value != null && _value.Equals(value))
                    return;
                if (PropertyChanging != null)
                    PropertyChanging(this, new PropertyChangingEventArgs("Value"));
                _value = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Value"));
            }
        }

        public override string ToString()
        {
            return Value;
        }
        protected override void Persistent_OnRecoverContextValue(string ctxValue)
        {
            _value = ctxValue;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public event PropertyChangingEventHandler PropertyChanging;
    }

    public class Context<T> : ContextBase, INotifyPropertyChanged, INotifyPropertyChanging where T : struct
    {
        public Context(sm.SiteContext persistent)
            : base(persistent)
        {

        }

        private T _value;
        public T Value
        {
            get
            {
                return _value;
            }
            set
            {
                if (_value.Equals(value))
                    return;
                if (PropertyChanging != null)
                    PropertyChanging(this, new PropertyChangingEventArgs("Value"));
                _value = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Value"));
            }
        }

        public override string ToString()
        {
            return _value.ToString();
        }

        protected override void Persistent_OnRecoverContextValue(string ctxValue)
        {
            try
            {
                _value = (T)Convert.ChangeType(ctxValue, typeof(T));
            }
            catch
            {

            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        public event PropertyChangingEventHandler PropertyChanging;
    }

}
